package com.example.opengldemo.simpleGame.addRigidBody;

import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.CompoundShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;
import com.example.opengldemo.simpleGame.catcherFun.MySurfaceView;
import com.example.opengldemo.simpleGame.object.BNAbstractDoll;
import com.example.opengldemo.simpleGame.object.LoadedObjectVertexNormalTexture;
import com.example.opengldemo.simpleGame.util.RigidBodyHelper;

import javax.vecmath.Vector3f;

import static com.example.opengldemo.simpleGame.constant.SourceConstant.*;

public class Doll extends BNAbstractDoll {
	
		DiscreteDynamicsWorld dynamicsWorld;
		int texId;
		
		CollisionShape body;
		BoxShape foot;
		BoxShape nose;
		CollisionShape[]  pigbody=new CollisionShape[5];
		CollisionShape  pigshpae;
		
		Vector3f position;
		
		

		MySurfaceView mv;
		public Doll(int texId, DiscreteDynamicsWorld dynamicsWorld,
					LoadedObjectVertexNormalTexture lovo, Vector3f position, int bianhao)
		{
			this.dynamicsWorld=dynamicsWorld;
			this.lovo=lovo;
			this.texId=texId;
			this.position=position;
			this.bianhao=bianhao;
			initRigidBodys();
		}
		public void initRigidBodys()
		{

			body=new BoxShape(new Vector3f(bodyc,bodyg,bodyk)); 
 			foot=new BoxShape(new Vector3f(footc,footg,footk));
			nose=new BoxShape(new Vector3f(nosec,noseg,nosek));
			pigbody[0]=body;
			pigbody[1]=nose;
			pigbody[2]=foot;
			pigbody[3]=new BoxShape(new Vector3f(bodyadd1x,bodyadd1y,bodyadd1z));
			pigbody[4]=new BoxShape(new Vector3f(bodyadd2x,bodyadd2y,bodyadd2z));
			pigshpae=addChild(pigbody);
			
			RigidBodydoll= RigidBodyHelper.addRigidBody(1,pigshpae,position.x,position.y,position.z,dynamicsWorld,false);
		}
 	  	public CompoundShape addChild(CollisionShape[] shape)
	  	{
	  		CompoundShape comShape=new CompoundShape();
	  		
	  		Transform localTransform = new Transform();
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(0,bodyg+footg*2,0));
 	  		comShape.addChildShape(localTransform, shape[0]);
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(0,bodyg+footg*2,bodyk));
	  		comShape.addChildShape(localTransform, shape[3]);
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(0,bodyg+footg*2,-bodyk));
	  		comShape.addChildShape(localTransform, shape[3]);
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(0,bodyg*2+footg*2,0));
	  		comShape.addChildShape(localTransform, shape[4]);
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(bodyc+nosec,bodyg+footg*2,0));
	  		comShape.addChildShape(localTransform, shape[1]);
	  		

	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(bodyc-footc*2,footg,bodyk-footk*2));
	  		comShape.addChildShape(localTransform, shape[2]);
	  		
	  		
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(bodyc-footc*2,footg,-bodyk+footk*2));
	  		comShape.addChildShape(localTransform, shape[2]);
	  		
	  		
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(-bodyc+footc*2,footg,-bodyk+footk*2));
	  		comShape.addChildShape(localTransform, shape[2]);
	  		
	  		
	  		
	  		localTransform.setIdentity();
	  		localTransform.origin.set(new Vector3f(-bodyc+footc*2,footg,bodyk-footk*2));
	  		comShape.addChildShape(localTransform, shape[2]);

	  		
	  		
	  		return comShape;
	  	}
		public void drawSelf()
		{
			
		    	MatrixState3D.pushMatrix();
				Transform trans=RigidBodydoll.getMotionState().getWorldTransform(new Transform());
				trans.origin.y=trans.origin.y-speed;
				MatrixState3D.translate(trans.origin.x,trans.origin.y, trans.origin.z);
				trans.getOpenGLMatrix(MatrixState3D.getMMatrix());
				
				MatrixState3D.pushMatrix();
				MatrixState3D.translate(0.1f,0,0);
				MatrixState3D.scale(pigbl, pigbl, pigbl);
				lovo.drawSelf(texId);
				MatrixState3D.popMatrix();
				MatrixState3D.popMatrix();
			
		}
}
